package com.sojoys.artifact.manager;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import javax.sql.DataSource;

/**
 * @author : DengYing
 * @CreateDate : 2017年4月6日 下午6:31:17
 * @Description ：关系型数据源管理器
 */
public class DataSourceManager {
	
	private DataSourceManager() {
	}

	private static DataSourceManager ME = new DataSourceManager();

	public static DataSourceManager me() {
		return ME;
	}

	/**
	 * 生成器构建的数据模型使用的
	 */
	DataSource mainSources;
	DataSource configSources;
	DataSource buildSources;
	DataSource logSources;
	/**
	 * 扩展
	 */
	Map<String, DataSource> dataSources;

	public DataSource getMainSources() {
		return mainSources;
	}

	public void setMainSources(DataSource mainSources) {
		this.mainSources = mainSources;
	}

	public DataSource getConfigSources() {
		return configSources;
	}

	public void setConfigSources(DataSource configSources) {
		this.configSources = configSources;
	}

	public DataSource getBuildSources() {
		return buildSources;
	}

	public void setBuildSources(DataSource buildSources) {
		this.buildSources = buildSources;
	}

	public DataSource getLogSources() {
		return logSources;
	}

	public void setLogSources(DataSource logSources) {
		this.logSources = logSources;
	}

	public Map<String, DataSource> getDataSources() {
		return dataSources;
	}

	public void setDataSources(Map<String, DataSource> dataSources) {
		this.dataSources = dataSources;
	}

	public DataSource getDataSource(String name) {
		return dataSources.get(name);
	}

	public Connection getConnection(DataSource dataSource) {
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Connection getMainConnection() {
		return getConnection(mainSources);
	}

	public Connection getConfigConnection() {
		return getConnection(configSources);
	}

	public Connection getBuildConnection() {
		return getConnection(buildSources);
	}

	public Connection getLogConnection() {
		return getConnection(logSources);
	}

}
